package com.lxt;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author IvXuTao
 * @Package com.lxt.config
 * @date 2022/5/17 16:12
 */
@Configuration
public class CorsConfig {

    public CorsConfig() {

    }

    @Bean
    public CorsFilter corsFilter() {
        //1.添加cors的配置信息
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("http://localhost:8080");
        config.addAllowedOrigin("http://106.55.151.226:8080");
        config.addAllowedOrigin("www.mtv.com");
        config.addAllowedOrigin("www.mtv.com:8080");
        config.addAllowedOrigin("www.music.com");
        config.addAllowedOrigin("www.music.com:8080");
        config.addAllowedOrigin("*");
        //config.addAllowedOrigin("http://xxx.xxx.com");

        //  设置是否发送cookie信息
        config.setAllowCredentials(true);
        //  设置允许请求的方式
        config.addAllowedMethod("*");
        //  设置允许的header
        config.addAllowedHeader("*");

        //2.为url添加映射路径
        UrlBasedCorsConfigurationSource corsSource = new UrlBasedCorsConfigurationSource();
        corsSource.registerCorsConfiguration("/**", config);

        //3.返回重新定义好的corsSource
        return new CorsFilter(corsSource);

    }
}
